package com.ruoyi.system.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.SysNoticeVo;
import com.ruoyi.system.domain.bo.SysNoticeBo;
import com.ruoyi.system.service.ISysNoticeService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 通知公告
 *
 * @author ruoyi
 * @date 2023-11-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/notice")
public class SysNoticeController extends BaseController {

    private final ISysNoticeService iSysNoticeService;

    /**
     * 查询通知公告列表
     */
    @SaCheckPermission("system:notice:list")
    @GetMapping("/list")
    public TableDataInfo<SysNoticeVo> list(SysNoticeBo bo, PageQuery pageQuery) {
        return iSysNoticeService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出通知公告列表
     */
    @SaCheckPermission("system:notice:export")
    @Log(title = "通知公告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysNoticeBo bo, HttpServletResponse response) {
        List<SysNoticeVo> list = iSysNoticeService.queryList(bo);
        ExcelUtil.exportExcel(list, "通知公告", SysNoticeVo.class, response);
    }

    /**
     * 获取通知公告详细信息
     *
     * @param noticeId 主键
     */
    @SaCheckPermission("system:notice:query")
    @GetMapping("/{noticeId}")
    public R<SysNoticeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long noticeId) {
        return R.ok(iSysNoticeService.queryById(noticeId));
    }

    /**
     * 新增通知公告
     */
    @SaCheckPermission("system:notice:add")
    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysNoticeBo bo) {
        return toAjax(iSysNoticeService.insertByBo(bo));
    }

    /**
     * 修改通知公告
     */
    @SaCheckPermission("system:notice:edit")
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysNoticeBo bo) {
        return toAjax(iSysNoticeService.updateByBo(bo));
    }

    /**
     * 删除通知公告
     *
     * @param noticeIds 主键串
     */
    @SaCheckPermission("system:notice:remove")
    @Log(title = "通知公告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{noticeIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] noticeIds) {
        return toAjax(iSysNoticeService.deleteWithValidByIds(Arrays.asList(noticeIds), true));
    }
}
